﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using CloudSoft.Workflows;

namespace ERPStore.NewsLetters.Workflows.Activities
{
	public sealed class SavePendingItem : CodeActivity
	{
		public InArgument<ERPStore.NewsLetters.Models.PendingItem> PendingItem { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			var pendingItem = context.GetValue(this.PendingItem);
			var newsLetterService = context.GetService<Services.NewsLettersService>();

			var existing = newsLetterService.GetPendingItemByCampaignIdAndRecipientId(pendingItem.CampaignId, pendingItem.EmailRecipientId);
			if (existing != null)
			{
				return;
			}
			try
			{
				newsLetterService.SavePendingItem(pendingItem);
			}
			catch (Exception ex)
			{
				var logger = context.GetService<ERPStore.Logging.ILogger>();
				logger.Error(ex);
			}
		}
	}
}
